package in.smartbillpay.web.controllers;

import in.smartbillpay.web.models.City;
import in.smartbillpay.web.models.District;
import in.smartbillpay.web.services.AuthorityService;
import in.smartbillpay.web.services.LocationService;
import in.smartbillpay.web.validators.DistrictValidator;
import in.smartbillpay.web.validators.StateValidator;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/public")
public class GenericServicesController {
	
	private static final Logger logger = LoggerFactory.getLogger(GenericServicesController.class);
	
	@Autowired
	private AuthorityService authorityService;
	
	@Autowired
	private LocationService locationService;
	
	@Autowired
	private StateValidator stateValidator;
	
	@Autowired
	private DistrictValidator districtValidator;
	
	/**
	 * @return the logger
	 */
	public static Logger getLogger() {
		return logger;
	}

	/**
	 * Simply selects list of districts in a give state and returns the JSON response
	 */	
	@RequestMapping(value = "/locations/districts/state/{stateId}", method = RequestMethod.GET)
	public @ResponseBody List<District> getDistricts(@PathVariable int stateId) {
		logger.info("Inside getDistricts()");
		logger.info("stateId is -" + Integer.toString(stateId));
		
		List<District> districtList = locationService.getDistricts(stateId);
		logger.info("count of records -" + Integer.toString(districtList.size()));
		
		return districtList;
	}
	
	/**
	 * Simply selects list of cities in a give district and returns the JSON response
	 */	
	@RequestMapping(value = "/locations/city/district/{districtId}", method = RequestMethod.GET)
	public @ResponseBody List<City> getCitiess(@PathVariable int districtId) {
		logger.info("Inside getCitiess()");
		logger.info("districtId is -" + Integer.toString(districtId));
		
		List<City> cityList = locationService.getCities(districtId);
		logger.info("count of records -" + Integer.toString(cityList.size()));
		
		return cityList;
	}
	
	
	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}

	/**
	 * @return the locationService
	 */
	public LocationService getLocationService() {
		return locationService;
	}

	/**
	 * @param locationService the locationService to set
	 */
	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	/**
	 * @return the stateValidator
	 */
	public StateValidator getStateValidator() {
		return stateValidator;
	}

	/**
	 * @param stateValidator the stateValidator to set
	 */
	public void setStateValidator(StateValidator stateValidator) {
		this.stateValidator = stateValidator;
	}

	/**
	 * @return the districtValidator
	 */
	public DistrictValidator getDistrictValidator() {
		return districtValidator;
	}

	/**
	 * @param districtValidator the districtValidator to set
	 */
	public void setDistrictValidator(DistrictValidator districtValidator) {
		this.districtValidator = districtValidator;
	}
	
	
}
